<!--侧边栏菜单-->
<template>
  <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
           :collapse="isCollapse" background-color="#545c64" text-color="#fff" active-text-color="#00A2E8">
    <div style="height: 60px;line-height: 60px;text-align: center">
      <img src="../assets/logo.png" alt="" style="width: 20px;position: relative;top: 5px;margin-right: 5px">
      <b style="color: white" v-show="logoTextShow">后台管理系统</b>
    </div>
    <el-menu-item @click="clickItem(item)" v-for="item in noChildren" :key="item.name" :index="item.name">
      <!-- 字体图标,用模板字符串拼接,动态绑定 -->
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.name" :index="item.name">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group  v-for="subItem in item.children" :key="subItem.name">
        <el-menu-item :index="subItem.name" @click="clickItem(subItem)">{{ subItem.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu{
  height:100vh;

  h3{
    text-align: center;
    line-height: 48px;
    color: #fff;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
import MenuData from '../data/MenuData'
export default {
  data() {
    return {
      MenuData,
      logoTextShow:false
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      //console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      //console.log(key, keyPath);
    },
    clickItem(item){
      // 防止自己跳自己的报错
      if(this.$route.path!==item.path&&!(this.$route.path==='/home'&&(item.path==='/'))){
        this.$router.push(item.path)
      }
    }
  },
  computed: {
    // 放到计算属性,自动计算
    isCollapse(){
      this.logoTextShow = !this.logoTextShow;
      return this.$store.state.tab.isCollapse
    },
    noChildren() {
      // 如果没有children则返回true,会被过滤器留下
      return this.MenuData.filter(item => !item.children)
    },
    hasChildren() {
      return this.MenuData.filter(item => item.children)
    },

  }
}
</script>
